package com.example.library.controller;

import com.example.library.entity.Lib;
import com.example.library.mapper.LibMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller

@SessionAttributes("libs")

@RequestMapping("/library")
public class LibController {
    @Resource
    LibMapper libMapper;

    @GetMapping("/list")
    String list(Model model) {
        //model.addAttribute("libs", libs);
        List<Lib> libs = libMapper.selectList(null);
        model.addAttribute("libs", libs);
        return "list";  //返回名为"list"的视图，即templates中的list.html
    }
    @GetMapping("/add")
    String addForm() {
        return "add"; //返回名为"add"的视图，即templates中的add.html
    }
    @PostMapping("/add")
    String add(@RequestParam long id,@RequestParam String picture,@RequestParam String name,@RequestParam String author,@RequestParam String chubanshe,@RequestParam String riqi,@RequestParam String number) {
        Lib lib = new Lib();
        lib.setId(id);
        lib.setPicture(picture);
        lib.setName(name);
        lib.setAuthor(author);
        lib.setChubanshe(chubanshe);
        lib.setRiqi(riqi);
        lib.setNumber(number);
        libMapper.insert(lib);
        return "sucess"; //返回名为"success"的视图，即templates中的success.html
    }
    @GetMapping("/delete")
    String delete(@RequestParam int id) {

        libMapper.deleteById(id);
        return "sucess";
    }
    @GetMapping("/edit")
    String editForm(Model model,@RequestParam int id)
    {
        Lib lib = libMapper.selectById(id);
        model.addAttribute("lib", lib);
        //model.addAttribute("id", id);
        return "edit";//返回名为"edit"的视图，即templates中的edit.html
    }
//
//
//
    @PostMapping("/edit")
    String edit(@ModelAttribute Lib lib) {
//        libMapper.update(lib,null);
        libMapper.updateById(lib);
        //int updateById(@Param(Constants.ENTITY) T entity);
        return "sucess";
    }

    @RequestMapping("/tologin")
    String tologin() {
        return "login";  //返回名为"list"的视图，即templates中的list.html
    }
    @RequestMapping("user")
    String login(Model model,@RequestParam String username,@RequestParam String password,HttpServletRequest request) {
        HttpSession session =request.getSession();
        if("adminer".equals(username)&&"123".equals(password)){
            System.out.println(username);
            System.out.println(password);
            //model.addAttribute("username",username);
            session.setAttribute("username",username);
            return "sucess";
        }
        //model.addAttribute("user",user);
        System.out.println(username);
        System.out.println(password);
        System.out.println("not true");
        return "login";  //返回名为"list"的视图，即templates中的list.html
    }



}

